//
//  ComplainController.m
//  智慧小区
//
//  Created by lab603 on 15/6/30.
//  Copyright (c) 2015年 Dreamsoul. All rights reserved.
//

#import "ComplainController.h"
#import "Complain.h"
#import "ComplainFrame.h"
#import "MJExtension.h"
#import "PPHttpTool.h"
#import "APIClient.h"
#import "MJRefresh.h"
#import "Tool.h"
#import "ComplainHistoryCell.h"
#import "ComplainDetailController.h"
#import "ComplainNewController.h"
@interface ComplainController ()
//请求数据的页面值
@property (nonatomic,assign)int page;
//暂时保存的投诉建议数组
@property (nonatomic,strong)NSMutableArray *complainArray;
@end

@implementation ComplainController
- (NSMutableArray *)complainArray{
    if (_complainArray==nil) {
        _complainArray = [NSMutableArray array];
    }
    return _complainArray;
}
- (id)init{
    self = [super init];
    if (self) {
        self.title = @"反馈列表";
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewComplain)];
        self.view.backgroundColor = APP_BACK_COLOR;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化页面值
    self.page = 1;
    //获得投诉历史记录
    [self getComplainData];
    //添加下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getComplainData)];
    //添加上拉刷新
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreComplainData)];
}
/**
 *  获取更多的投诉历史记录
 */
- (void)getMoreComplainData{
    NSString *urlString = [APIClient relativePathForComplainHistory];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page"] = @(++self.page);
    params[@"size"] = @10;
    [PPHttpTool getWithURL:urlString params:params success:^(id json) {
        IWLog(@"%@",json);
        NSArray *tempArray = [Complain objectArrayWithKeyValuesArray:json];
        if (tempArray.count==0) {
            [Tool HUDShowMessage:@"没有更多记录..."];
            [self.tableView.footer endRefreshing];
            return ;
        }
        for (int i=0; i<tempArray.count; i++) {
            Complain *complain = tempArray[i];
            ComplainFrame *complainF = [[ComplainFrame alloc] init];
            complainF.complain = complain;
            [self.complainArray addObject:complainF];
        }
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.footer endRefreshing];
    }];
}
/**
 *  获取投诉历史记录
 */
- (void)getComplainData{
    NSString *urlString = [APIClient relativePathForComplainHistory];
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    //初始化分页值
    self.page = 1;
    params[@"page"]=@(self.page);
    params[@"size"]= @10;
    [PPHttpTool getWithURL:urlString params:params success:^(id json) {
        IWLog(@"%@",json);
        NSArray *tempArray = [Complain objectArrayWithKeyValuesArray:json];
        [self.complainArray removeAllObjects];
        for (int i=0; i<tempArray.count; i++) {
            Complain *complain = tempArray[i];
            ComplainFrame *complainF = [[ComplainFrame alloc] init];
            complainF.complain = complain;
            [self.complainArray addObject:complainF];
        }
        if ([self.tableView.header isRefreshing]) {
            [self.tableView.header endRefreshing];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        if ([self.tableView.header isRefreshing]) {
            [self.tableView.header endRefreshing];
        }
    }];
}
/**
 *  新增一条投诉建议
 */
- (void)addNewComplain{
    ComplainNewController *complainNewVC = [[ComplainNewController alloc] init];
    [self.navigationController pushViewController:complainNewVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/**
 *  提醒物业处理
 */
- (void)remind{
    [Tool HUDShowMessage:@"提醒已发送，请稍候..."];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.complainArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ComplainHistoryCell *cell = [ComplainHistoryCell cellWthTableView:tableView];
    cell.complainF = self.complainArray[indexPath.row];
    [cell setTapRemindBtnBlock:^{
        //提醒按钮被点击
        [self remind];
    }];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ComplainFrame *complainF = self.complainArray[indexPath.row];
    return complainF.cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ComplainFrame *complainF = self.complainArray[indexPath.row];
    Complain *complain = complainF.complain;
    ComplainDetailController *complainDetailVC = [[ComplainDetailController alloc] init];
    complainDetailVC.complain = complain;
    [self.navigationController pushViewController:complainDetailVC animated:YES];
}
@end
